package com.lrkj.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.StartUpOnLoadData;
import com.lrkj.back.service.TbSeqService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.Const;
import com.lrkj.back.entity.TbDict;
import com.lrkj.back.entity.TbEmployee;
import com.lrkj.back.entity.ZsWanglan;
import com.lrkj.back.service.ZsWanglanService;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/wanglan")
public class ZsWanglanController extends BaseController{
	@Autowired
	private ZsWanglanService zsWanglanService;

	@Autowired
	private TbSeqService tbSeqService;

	//配置--网篮管理     （点击查询）
	@RequestMapping(value="/wanglanList.do")
	public ModelAndView wanglanList(String searchTxt,HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("wlTiaoma", searchTxt.trim());
			map.put("wlMingcheng", searchTxt.trim());
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsWanglan> page = zsWanglanService.findAllWanglanList(map);
		request.setAttribute("wanglanList", page.getList());
		request.setAttribute("dictMap", StartUpOnLoadData.singleDataMap);
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt.trim());
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/peizhi/wanglan/wanglan_list");
	}
		
	@RequestMapping("/wanglanEdit.do")
	public ModelAndView wanglanEdit(String  wlTiaoma, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		ZsWanglan zsWanglan = (wlTiaoma == null) ? new ZsWanglan() : zsWanglanService.selectByPrimaryKey(wlTiaoma);
		if(wlTiaoma == null){
			zsWanglan.setIsReadOnly(0);
		}
		List<TbDict> wlProperty = StartUpOnLoadData.dataListMap.get(StartUpOnLoadData.MIEJUN_WLSX);
		request.setAttribute("wlprop", wlProperty);
		request.setAttribute("zsWanglan", zsWanglan);
		return new ModelAndView("/peizhi/wanglan/wanglan_edit");
		
	}
	@RequestMapping(value="/wanglanSave.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<Object, Object> wanglanSave(ZsWanglan zsWanglan ,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		TbEmployee emp = (TbEmployee)request.getSession().getAttribute("emp");
		Map<Object, Object> rtnMap = new HashMap<>();
		try {

			Long no = tbSeqService.seqVal("wlno");
			if(StringUtils.isBlank(zsWanglan.getWlTiaoma()) ){
				String wltm = "WL"+0+no;
				zsWanglan.setWlTiaoma(wltm);
				zsWanglan.setWlCreatetime(new Date());
				zsWanglan.setCreator(emp.getEmployeeName());
				zsWanglan.setCreatorId(emp.getEmployeeId());
				zsWanglan.setWlState(0);
				zsWanglanService.insert(zsWanglan);
			}else{
				zsWanglanService.updateByPrimaryKey(zsWanglan);
			}

			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败,可能存在相同条码");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping(value="/wanglanDel.do")
	@ResponseBody
	public Map<Object, Object> wanglanDel(HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			String wlTiaomaPram = request.getParameter("wlTiaoma");
			String[] wlTiaomas = wlTiaomaPram.split(",");
			for(String wlTiaoma : wlTiaomas){
				zsWanglanService.deleteByPrimaryKey(wlTiaoma);
				//packageproductService.deleteByPackageId(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
}